package com.c.yinyuezhushou.Fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.c.yinyuezhushou.Adapter.HomeManagerRecAdater;
import com.c.yinyuezhushou.Interface.OneInterface;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.MyTool.Immersed;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.Service.MusicService;
import com.c.yinyuezhushou.Ui.ListActivity;
import com.c.yinyuezhushou.Ui.SearchActivity;


import java.util.ArrayList;
import java.util.List;

public class MusicFragment extends Fragment {
    private View rootView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RelativeLayout relativeLayout;
    private ImageView imageView;
    private LinearLayout linearLayout;
    private TextView textView;
    private MusicService iBinder;
    private MyApplication myApplication;
    private Activity activity;

    public MusicFragment(MusicService iBinder,Activity activity){

        this.activity=activity;
        this.iBinder = iBinder;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_music, container, false);
        swipeRefreshLayout = rootView.findViewById(R.id.music_fragment_SRL);
        relativeLayout = rootView.findViewById(R.id.music_fragment_re);
        imageView = rootView.findViewById(R.id.music_fragment_image);
        textView = rootView.findViewById(R.id.music_fragment_text);
        linearLayout = rootView.findViewById(R.id.music_fragment_Linlayout);
        return rootView;
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Immersed.setImmersed(linearLayout,getActivity(),0);//设置状态栏

        myApplication = (MyApplication)getActivity().getApplicationContext();

        setOnClickListener();

        swipeRefreshLayout.setRefreshing(true);

        setView();
    }

    private void setOnClickListener(){
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                myApplication.setFragment(new DownFragment());
                Intent intent = new Intent(getActivity(), ListActivity.class);
                intent.putExtra("title","下载管理");
                getActivity().startActivity(intent);
            }
        });

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            setView();
                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    }
                },500);
            }
        });
    }

    public void setView(){
        HomeFragment3 homeFragment3 = new HomeFragment3(new OneInterface() {
            @Override
            public void back() {
                if (swipeRefreshLayout!=null){
                    swipeRefreshLayout.setRefreshing(false);
                }
            }
        });
        final FragmentTransaction ft=this.getFragmentManager().beginTransaction();
        ft.add(R.id.music_fragment_re,homeFragment3);
        ft.show(homeFragment3);

        ft.commit();
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
//        recyclerView.setLayoutManager(linearLayoutManager);
//
//        List<Fragment> list = new ArrayList<Fragment>();
//
//        list.add(new HomeFragment3(new OneInterface() {
//            @Override
//            public void back() {
//                if (swipeRefreshLayout!=null){
//                    swipeRefreshLayout.setRefreshing(false);
//                }
//            }
//        }));
//
//        recyclerView.setAdapter(new HomeManagerRecAdater(list,getFragmentManager()));
    }

}
